package com.module.antiFake.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import base.dao.GenericHibernateDao;
import base.util.Page;

import com.module.antiFake.vo.AntiFakeComplain;
import com.module.antiFake.vo.AntiFakeReply;
import com.module.antiFake.vo.AntiFakeWarn;

public class AntiFakeReplyDao extends GenericHibernateDao<AntiFakeReply,Integer>{
	private AntiFakeReplyDao(){}
	private static AntiFakeReplyDao dao;
	private AntiFakeComplainDao antiFakeComplainDao = AntiFakeComplainDao.getInstance();
	private AntiFakeWarnDao warnDao = AntiFakeWarnDao.getInstance();
	public synchronized static AntiFakeReplyDao getInstance(){
		if(dao == null){
			dao = new AntiFakeReplyDao();
		}
		return dao;
	}
	
	@SuppressWarnings("unchecked")
	public List<AntiFakeReply> findByFkId(Session session,Integer fkid) {
		String hql =" from AntiFakeReply as a where a.fkId="+fkid +" ORDER BY replyDate DESC";
		Query query = session.createQuery(hql);
		List<AntiFakeReply> list=query.list();
		return list;
	}
	
	
	public Page listAntiFakeReply(Session session,AntiFakeReply bean,Integer start, Integer limit ){
		Page page = new Page();
		String hql1 =" select b.complainName,b.complainConten,b.complainDate,b.id from AntiFakeComplain as b where b.id="+bean.getFkId();//投诉信息
		String hql2 =" from AntiFakeReply as a where a.fkId="+bean.getFkId();//所有的回复信息
		String hql3 =" select count(*) from AntiFakeReply where fkId="+bean.getFkId();
		List result=new ArrayList();//返回的结果集
		if(start==0){
			Query query=session.createQuery(hql1);
			List list=query.list();
			for(int i=0;i<list.size();i++){
				Object[] o=(Object[])list.get(i);
				AntiFakeReply temp = new AntiFakeReply();
				
				temp.setReplyName(o[0].toString());
				temp.setReplyContent(o[1].toString());
				temp.setReplyDate((Date)o[2]);
				temp.setFkId((Integer)o[3]);
				result.add(temp);
			}
		}
		Query query2=session.createQuery(hql2);
		query2.setFirstResult(start).setMaxResults(limit-1);
		List listReply=query2.list();
		for(int i=0;i<listReply.size();i++){
			AntiFakeReply ar = (AntiFakeReply)listReply.get(i);
			result.add(ar);
		}
		page.setTotal((long) ((Long) session.createQuery(hql3).uniqueResult()).intValue()+1);
		page.setRows(result);	
		return page;
	}
	

	public boolean addAntiFakeReply(Session session,AntiFakeReply bean,String state){
        boolean flag = false;
        save(session,bean);
        AntiFakeComplain antiFakeComplain=antiFakeComplainDao.get(session,bean.getFkId());
        antiFakeComplain.setState(state);
		flag = antiFakeComplainDao.update(session,antiFakeComplain);
		if(antiFakeComplain.getWarnId()!=null){
			AntiFakeWarn antiFakeWarn = warnDao.get(session,new Long(antiFakeComplain.getWarnId()));
    		if(antiFakeWarn!=null){
    			antiFakeWarn.setProcessState(antiFakeWarn.PROCESS_STATE_COMPLETE);
        		antiFakeWarn.setProcessId(bean.getReplyId());
        		antiFakeWarn.setProcessTime(new Date());
        		antiFakeWarn.setProcessName(bean.getReplyName());
        		flag=warnDao.update(session,antiFakeWarn);
    		}
		}
        flag = true;
        return flag;
	}
	
}
